package com.example.spring.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.spring.entity.Worker;
import com.example.spring.mapper.WorkerMapper;
import com.example.spring.service.WorkerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/worker")
public class WorkerController {

    @Autowired
    private WorkerMapper workerMapper;

    @Autowired
    private WorkerService workerService;

    //登录
    @PostMapping("/login")
    public Map<String, Object> login(@RequestBody Worker worker)
    {
        List<Worker> valid_worker =  workerMapper.valid_worker(worker.getUsername());
        List<Worker> valid_password = workerMapper.valid_match_password(worker.getUsername(), worker.getPassword());
        Integer result;
        if (valid_worker != null && ! valid_worker.isEmpty())
        {
            if(valid_password != null && !valid_password.isEmpty())
            {
                result = 1;//正确识别
            }
            else
            {
                result = 3;//密码不对
            }
        }
        else
        {
            result = 2;//用户名不存在
        }
        Map<String, Object> map = new HashMap<>();
        if (result == 1) {
            map.put("wkid", valid_password.get(0).getWkid());
            map.put("whid", valid_password.get(0).getWhid());
        }
        map.put("status", result);
        return map;
    }
    // 查询个人信息
    @GetMapping("/{wkid}")
    public Worker findOne(@PathVariable Integer wkid)
    {
        return workerMapper.find_by_wkid(wkid);
    }

    //新增或修改
    @PostMapping()
    public Integer save(@RequestBody Worker worker) {
        //新增或者更新
        return workerService.save(worker);
    }
    //查询所有数据
    @GetMapping()
    public List<Worker> findAll() {
        List<Worker> all =  workerMapper.findAll();
        return all;
    }

    //按id删除
    @DeleteMapping("/{wkid}")
    public Integer delete(@PathVariable Integer wkid)
    {
        return workerMapper.deleteById(wkid);
    }

    // 分页查询
    // 接口路径：/worker/page?pageNum=1&pageSize=10
    // @RequestParam接受
    // limit第一个参数 = (pageNum - 1) * pageSize
    // pageSize
    @GetMapping("/page")
    public Map<String, Object> findPage(@RequestParam Integer pageNum,
                                        @RequestParam Integer pageSize) {
        pageNum = (pageNum - 1) * pageSize;
        List<Worker> data = workerMapper.selectPage(pageNum, pageSize);
        Integer total = workerMapper.selectTotal();
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }
    //忘记密码验证用户名手机电话
    @PostMapping("/forgetpassword")
    public Integer forgetPassword(@RequestBody Worker worker)
    {
        Integer worker1 = workerService.find_password(worker);
        return worker1;
    }

    //修改指定用户的密码，需要提供wkid
    @PostMapping("/password")
    public Integer changePassword(@RequestBody Worker worker) {
        if (worker.getWkid() == 0 || worker.getWkid() == null || StringUtils.isBlank(worker.getPassword()))
        {
            return 0;
        }
        Worker worker1 = workerMapper.find_by_wkid(worker.getWkid());
        worker1.setPassword(worker.getPassword());
        return workerMapper.update(worker1);
    }
    //返回用户信息
    @PostMapping("/information")
    public Worker show_info(@RequestParam Integer wkid)
    {
        return workerMapper.find_by_wkid(wkid);
    }

    //修改个人信息
    @PostMapping("/changeInfo")
    public Integer change_info(@RequestBody Worker worker){
        Integer res = workerMapper.update(worker);
        return  res;
    }
}
